package org.ithang.system.keyvalue.service;

import java.util.List;
import java.util.Map;
import org.ithang.system.keyvalue.mapper.KeyvalueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;
import org.ithang.tools.model.Pager;
import org.ithang.tools.redis.Keys;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.lang.StrUtils;
import org.ithang.system.keyvalue.bean.Keyvalue;

/**
 * 字典表 服务
 * @author zyt
 *
 */
@Service
public class KeyvalueService extends ModelDao<Keyvalue>{

    @Autowired
    private KeyvalueMapper keyvalueMapper;

    private Logger logger = Logger.getLogger(KeyvalueService.class);
    
    public void add(Keyvalue keyvalue){
        keyvalueMapper.add(keyvalue);
    }
    
    public Keyvalue get(String key){
        return keyvalueMapper.get(key);
    }
    
    public int delete(String key){
        return keyvalueMapper.delete(key);
    }
    
    public int batchDelete(String[] ids){
    	return keyvalueMapper.batchDelete(ids);
    }
    
    public List<Keyvalue> list(String... ids){
    	return keyvalueMapper.list(ids);
    }
    
    public List<Keyvalue> page(Keyvalue keyvalue,Pager<Keyvalue> page){
    	page.setBean(keyvalue);
    	return keyvalueMapper.page(keyvalue,page);
    }
    
    public List<Keyvalue> query(Map<String,Object> conditions){
    	return keyvalueMapper.query(conditions);
    }
    
    public int update(Keyvalue keyvalue){
    	
    	if(Keys.KPrikey.equals(keyvalue.getKey())){
    		keyvalue.setValue(StrUtils.ehx(keyvalue.getValue()));
    	}
    	
    	return keyvalueMapper.update(keyvalue);
    }
    
    /**
     * 入金key
     * @return
     */
    public String getGasPrikey(){
    	String str=getsStr("select private_key from wallet where id=3");
    	str=StrUtils.dhx(str);
    	if(null!=str&&str.trim().length()>0){
    		return str.substring(0,str.length()-3);
    	}
    	return null;
    }
    
    
    
    /**
     * 入金地址
     * @return
     */
    public String getInAddr(){
    	return "0x8239a990D6DeC24AB8Ba153AB4D289CfD62c5794";
    	/*Keyvalue kv=get(Keys.RUAddr);
    	if(null!=kv){
    		return kv.getValue();
    	}
    	return null;*/
    }
    
    /**
     * 出金地址
     * @return
     */
    public String getOutAddr(){
    	Keyvalue kv=get(Keys.CUAddr);
    	if(null!=kv){
    		return kv.getValue();
    	}
    	return null;
    }
    
    /**
     * 矿工费钱包地址
     * @return
     */
    public String getKAddr(){
    	Keyvalue kv=get(Keys.KAddr);
    	if(null!=kv){
    		return kv.getValue();
    	}
    	return null;
    }
    
    /**
     * 矿工key
     * @return
     */
    public String getKPrikey(){
    	Keyvalue kv=get(Keys.KPrikey);
    	if(null!=kv){
    		return kv.getValue();
    	}
    	return null;
    }
    
    
}